# -*- coding: utf-8 -*-
# @Author: hunan
# @Date: 2024/11/20 14:04
# @Description:
import re
import json


def gen_json(filename, result_dict):
    # 持久化到 JSON 文件
    with open(filename, "w") as f:
        json.dump(result_dict, f, indent=4)


def load_json(filename):
    # 从 JSON 文件中读取字典
    with open(filename, "r") as f:
        return json.load(f)


def gen_data_definitions(data, dest_path):
    res_dict = {}
    for row_idx in range(len(data)):
        if data[row_idx][0] is None and data[row_idx][1] is not None:
            field_name = data[row_idx][1]
            if " - " in field_name:
                field_name = field_name.split(" - ")[0]
            tmp = {}
            definition_and_use = data[row_idx][3]
            accepted_values = data[row_idx][4]
            example = data[row_idx][5]
            tmp["Definition and Use"] = definition_and_use
            tmp["Accepted_Values"] = accepted_values
            tmp["Example"] = example
            res_dict.update({field_name: tmp})
    # print(json.dumps(res_dict))
    gen_json(dest_path, res_dict)


def required_field(data, dest_path):
    res = []
    for row_idx in range(len(data)):
        if data[row_idx][0] is None and data[row_idx][1] is not None and data[row_idx][6] is not None and data[row_idx][
            6] == "Required":
            field_name = data[row_idx][1]
            if " - " in field_name:
                field_name = field_name.split(" - ")[0]
            res.append(field_name)
    # print(json.dumps(res))
    gen_json(dest_path, res)


def field_map(data, dest_path):
    result_map = {}
    # print(data)
    for col_index in range(len(data[2])):
        human_name = data[1][col_index]
        name = data[2][col_index]
        if name[-1].isdigit():
            name = name[:-1] + '1'
        result_map.update({human_name: name})
    # print(json.dumps(result_map))
    gen_json(dest_path, result_map)
    return result_map


def template_filed(data, dest_path):
    res = []
    for col_index in range(len(data[1])):
        name = data[2][col_index]
        res.append(name)
    # print(json.dumps(res))
    gen_json(dest_path, res)


# 提取valid values规则,传入整个二维列表, 字段大小写映射, 生成json地址
def values(data, dest_path, field_map, drop_list_path):
    res = {}
    for row in range(len(data)):
        if data[row][0] is None:
            row_data = data[row]
            row_data = [x for x in row_data if x is not None]
            field_name = row_data[0].split(" - [ ")[0]
            if field_name[-1].isdigit():
                field_name = field_name[:-1]
            field_name = field_map.get(field_name)
            field_list = row_data[1:]
            # 遇到重复的了
            if res.get(field_name) is not None:
                drop_dict = load_json(drop_list_path)
                drop_list = drop_dict.get(field_name, [])
                pre_values_list = res.get(field_name)
                if len(drop_list) > 0:
                    if sorted(drop_list) == sorted(pre_values_list):
                        field_list = pre_values_list
                elif len(pre_values_list) > len(field_list):
                    field_list = pre_values_list

            res.update({field_name: field_list})
    # print(json.dumps(res))
    gen_json(dest_path, res)


# 提取drop规则,传入整个二维列表,生成的json地址
def drop(data, dest_path):
    dict_res = {}
    # 获取列数
    num_cols = len(data[0])
    # 按列循环操作
    for col in range(num_cols):
        column_data = [row[col] for row in data]  # 提取当前列的数据
        column_data = [x for x in column_data if x is not None]
        field_name = column_data[2]
        if field_name[-1].isdigit() and "bullet_point" not in field_name and "other_image_url" not in field_name:
            field_name = field_name[:-1] + '1'
        field_list = column_data[3:]
        dict_res.update({field_name: field_list})
    # print(json.dumps(dict_res))
    gen_json(dest_path, dict_res)


def get_product_type(data):
    return data[3][0]
